package com.raccoonagents.tools.cascet2d.model.particle;


import com.raccoonagents.tools.cascet2d.interfaces.IRenderableStageElement;
import com.raccoonagents.tools.cascet2d.interfaces.ISynchronizedUpdateable;
import com.raccoonagents.tools.cascet2d.interfaces.rendering.IRenderableObject;
import com.raccoonagents.tools.cascet2d.model.Vector2f;

public abstract class Particle implements IRenderableObject, ISynchronizedUpdateable{
	/**desired maximum lifetime in milliseconds*/
	protected long maxLife = 1000;
	/**desired lifetime in milliseconds*/
	protected long currentLife = maxLife;
	/** direction vector of movement*/
	protected Vector2f direction;
	/** size of particle */
	protected Vector2f size = new Vector2f(15.0f,15.0f);
	/** size of particle */
	protected Vector2f position = new Vector2f(0.0f,0.0f);
	/** if particle is not alive, it can be collected away.*/
	protected boolean isAlive = true;
	float [] color = {1.0f,1.0f,1.0f};
	protected float opacity = 0.4f;
	protected float speed = 0.0f;
	ParticleSystem system = null;
	protected int rotation = 0;
	protected long minLife = 0;
	protected boolean isActive = true;
	protected Vector2f pivot = new Vector2f(0.0f,0.0f);

	public boolean isActive() {
		return isActive;
	}

	@Override
	public Vector2f getPivot() {
		return pivot;
	}
	
	public void setActive(final boolean isActive) {
		this.isActive = isActive;
	}

	public long getMinLife() {
		return minLife;
	}

	public void setMinLife(final long minLife) {
		this.minLife = minLife;
	}

	public float getSpeed() {
		return speed;
	}

	public void setSpeed(final float speed) {
		this.speed = speed;
	}
	
	
	
	public ParticleSystem getSystem() {
		return system;
	}

	void setSystem(final ParticleSystem system) {
		this.system = system;
	}

	public void setSize(final Vector2f size) {
		this.size = size;
	}

	public void setColor(final float[] color) {
		this.color = color;
	}

	public void setOpacity(final float opacity) {
		this.opacity = opacity;
	}

	public long getMaxLife() {
		return maxLife;
	}

	public void setMaxLife(final long maxLife) {
		this.maxLife = maxLife;
	}
	@Override
	public void setLocation(final Vector2f location) {
		this.position.x = location.x;
		this.position.y = location.y;
	}

	@Override
	public void setParentElement(final IRenderableStageElement element) {		
	}

	public long getCurrentLife() {
		return currentLife;
	}

	public void setCurrentLife(final long currentLife) {
		this.currentLife = currentLife;
	}

	public Vector2f getDirection() {
		return direction;
	}

	public void setDirection(final Vector2f direction) {
		this.direction = direction;
	}

	public Vector2f getPosition() {
		return position;
	}

	public void setPosition(final Vector2f position) {
		this.position = position;
	}

	public boolean isAlive() {
		return isAlive;
	}
	/*
	public void setAlive(final boolean isAlive) {
		this.isAlive = isAlive;
	}*/

	public Particle(){
	}
	
	@Override
	public void update(final long time) {
		if(isActive){
			currentLife -= time;
			position.x += direction.x * time * speed;
			position.y += direction.y * time * speed;		
			if(currentLife < 0){
				isAlive = false;
				if(system != null){
					system.removeParticle(this);
				}
			}			
		}
		
	}

	@Override
	public final Vector2f getSize() {
		return size;
	}
	
	public  long getLife(){
		return currentLife;
	}
	
	public abstract Particle copy(ParticleSystem system);
	
	@Override
	public Vector2f getLocation() {
		return position;
	}

	@Override
	public IRenderableStageElement getParentElement() {
		return null;
	}

	@Override
	public Vector2f getWorldCoordinates(final Vector2f localCoordinates) {
		return position;
	}
	
	@Override
	public float[] getColor() {
		return color;
	}

	@Override
	public float getOpacity() {
		return opacity;
	}
	

	@Override
	public float getRotation() {
		return rotation;
	}

}
